package com.avic.rong.goodsSort.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.BaseActivity;
import com.avic.rong.BaseFragment;
import com.avic.rong.R;
import com.avic.rong.goodsSort.fragment.bz.GoodsBiaoZhunFragment;
import com.avic.rong.goodsSort.fragment.ts.GoodsTuShuFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 分类界面
 */

public class GoodsSortActivity extends BaseActivity {

    @BindView(R.id.item_Img1)
    TextView itemImg1;
    @BindView(R.id.item_Tv1)
    TextView itemTv1;
    @BindView(R.id.item_Img2)
    TextView itemImg2;
    @BindView(R.id.item_Tv2)
    TextView itemTv2;
    @BindView(R.id.item_Img3)
    TextView itemImg3;
    @BindView(R.id.item_Tv3)
    TextView itemTv3;
    @BindView(R.id.item_Img4)
    TextView itemImg4;
    @BindView(R.id.item_Tv4)
    TextView itemTv4;
    @BindView(R.id.goods_FrameLayout)
    FrameLayout goodsFrameLayout;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;

    private Fragment currentFragment = new Fragment();
    private GoodsBiaoZhunFragment biaoZhunFragment = new GoodsBiaoZhunFragment();
    private BaseFragment tuShuFragment = GoodsTuShuFragment.getInstance(2);
    private BaseFragment wenDangFragment = GoodsTuShuFragment.getInstance(3);
    private BaseFragment gongJuFragment = GoodsTuShuFragment.getInstance(4);
//    private GoodsWenDangFragment wenDangFragment = new GoodsWenDangFragment();
//    private GoodsGongJuFragment gongJuFragment = new GoodsGongJuFragment();


    @Override
    protected int getContentViewId() {
        return R.layout.activity_goods_sort;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        itemTv1.setSelected(true);
        int intExtra = getIntent().getIntExtra("分类", 0);
        //根据传过来的值去点击
        switch (intExtra) {
            case 1:
                //目前默认就是标准所以设置不设置都可以
                itemImg1.setClickable(true);
                break;
        }
        tvTitle.setText("商品分类");
        switchFragment(biaoZhunFragment);
    }


    @Override
    protected void addListener() {

    }


    @OnClick({R.id.item_Tv1, R.id.item_Tv2, R.id.item_Tv3, R.id.item_Tv4, R.id.iv_back, R.id.iv_right})
    public void onClick(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.item_Tv1:
//                fragment = new GoodsBiaoZhunFragment();
                switchFragment(biaoZhunFragment);
                itemImg1.setVisibility(View.VISIBLE);
                itemImg2.setVisibility(View.GONE);
                itemImg3.setVisibility(View.GONE);
                itemImg4.setVisibility(View.GONE);
//                itemTv1.setBackgroundColor(Color.WHITE);
//                itemTv2.setBackgroundColor(color_e9e9e9);
//                itemTv3.setBackgroundColor(color_e9e9e9);
//                itemTv4.setBackgroundColor(color_e9e9e9);
                itemTv1.setSelected(true);
                itemTv2.setSelected(false);
                itemTv3.setSelected(false);
                itemTv4.setSelected(false);
                itemTv1.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                itemTv2.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                itemTv3.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                itemTv4.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                break;
            case R.id.item_Tv2:
//                fragment = new GoodsTuShuFragment();
                switchFragment(tuShuFragment);
                itemImg1.setVisibility(View.GONE);
                itemImg2.setVisibility(View.VISIBLE);
                itemImg3.setVisibility(View.GONE);
                itemImg4.setVisibility(View.GONE);
//                itemTv1.setBackgroundColor(color_e9e9e9);
////                itemTv2.setBackgroundColor(Color.WHITE);
////                itemTv3.setBackgroundColor(color_e9e9e9);
////                itemTv4.setBackgroundColor(color_e9e9e9);
                itemTv1.setSelected(false);
                itemTv2.setSelected(true);
                itemTv3.setSelected(false);
                itemTv4.setSelected(false);
                itemTv1.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                itemTv2.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                itemTv3.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                itemTv4.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                break;
            case R.id.item_Tv3:
//                fragment = new GoodsWenDangFragment();
                switchFragment(wenDangFragment);
                itemImg1.setVisibility(View.GONE);
                itemImg2.setVisibility(View.GONE);
                itemImg3.setVisibility(View.VISIBLE);
                itemImg4.setVisibility(View.GONE);
//                itemTv1.setBackgroundColor(color_e9e9e9);
//                itemTv2.setBackgroundColor(color_e9e9e9);
//                itemTv3.setBackgroundColor(Color.WHITE);
//                itemTv4.setBackgroundColor(color_e9e9e9);
                itemTv1.setSelected(false);
                itemTv2.setSelected(false);
                itemTv3.setSelected(true);
                itemTv4.setSelected(false);
                itemTv1.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                itemTv2.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                itemTv3.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                itemTv4.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                break;
            case R.id.item_Tv4:
//                fragment = new GoodsGongJuFragment();
                switchFragment(gongJuFragment);
                itemImg1.setVisibility(View.GONE);
                itemImg2.setVisibility(View.GONE);
                itemImg3.setVisibility(View.GONE);
                itemImg4.setVisibility(View.VISIBLE);
//                itemTv1.setBackgroundColor(color_e9e9e9);
//                itemTv2.setBackgroundColor(color_e9e9e9);
//                itemTv3.setBackgroundColor(color_e9e9e9);
//                itemTv4.setBackgroundColor(Color.WHITE);
                itemTv1.setSelected(false);
                itemTv2.setSelected(false);
                itemTv3.setSelected(false);
                itemTv4.setSelected(true);
                itemTv1.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                itemTv2.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                itemTv3.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                itemTv4.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_right:
                showPopup(GoodsSortActivity.this, findViewById(R.id.goods_sort_linear));
                break;
        }
    }

    private void switchFragment(Fragment targetFragment) {
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        if (!targetFragment.isAdded()) {//如果要显示的targetFragment没有添加过
            transaction
                    .hide(currentFragment)//隐藏当前Fragment
                    .add(R.id.goods_FrameLayout, targetFragment, targetFragment.getClass().getName())//添加targetFragment
                    .commit();
        } else {//如果要显示的targetFragment已经添加过
            transaction//隐藏当前Fragment
                    .hide(currentFragment)
                    .show(targetFragment)//显示targetFragment
                    .commit();
        }
        //更新当前Fragment为targetFragment
        currentFragment = targetFragment;
    }


    public static Intent getIntent(Context context) {
        Intent intent = new Intent(context, GoodsSortActivity.class);
        return intent;
    }


}
